import { baseURL } from 'config/env.js'
import store from 'store/index.js'

async function request(config) {
  let userInfo = localStorage.getItem('token')
  let token = userInfo ? JSON.parse(userInfo)?.token : ''
  let timeout =config.timeout || 10000 //设置请求超时10秒钟
  config = { ...config, baseURL, token, timeout }
  if (config.loading !== false) {
    store.commit('XLoading/setLoadingState', {
      loading: true,
      loadingText: config.loadingText || '加载中...'
    })
  }
  try {
    let res = await elFunc.request(config).finally(() => {
      store.commit('XLoading/setLoadingState', { loading: false })
    })
    return Promise.resolve(res)
  } catch (err) {
    return Promise.reject(err)
  }
}

export default request
